package com.yunshipei.manager;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.net.Uri;
import android.text.TextUtils;

import com.yunshipei.common.Globals;
import com.yunshipei.db.DatabaseUtil;
import com.yunshipei.model.UserInfo;
import com.yunshipei.model.NotificationMessageModel;

import io.rong.app.utils.CommonNotification;
import io.rong.imkit.RongContext;
import io.rong.imlib.model.Message;
import io.rong.imlib.model.MessageContent;
import io.rong.message.ImageMessage;
import io.rong.message.LocationMessage;
import io.rong.message.RichContentMessage;
import io.rong.message.TextMessage;
import io.rong.message.VoiceMessage;

/**
 * Created by cike on 16/7/27.
 */
public class EnterplorerNotificationManager {

    private SharedPreferences mySharedPreferences = null;

    private Context context = null;
    private boolean shouldSound = false;

    private EnterplorerNotificationManager(Builder builder) {
        this.context = builder.context.getApplicationContext();
        this.shouldSound = builder.shouldSound;
        mySharedPreferences = context.getSharedPreferences("ysp_rongCloud", Context.MODE_PRIVATE);
    }

    public void showNotification(Message message) {
        //显示时
        if (Globals.MESSAGE_FRAGMENT_STATUS != 0)
            return;
        if (mySharedPreferences != null && !mySharedPreferences.getBoolean(Globals.SP_MESSAGE_ALERT_ENABLE, true)) {
            return;
        }

        DatabaseUtil databaseUtil = new DatabaseUtil();
        UserInfo userInfo = databaseUtil.queryUserInfoByid(message.getSenderUserId());
        String userName = "";
        if (userInfo != null) {
            userName = userInfo.getName();
        }
        String contentText = (TextUtils.isEmpty(userName) ? "" : userName + ":") + MessageType(message.getContent());
        NotificationMessageModel nmm = new NotificationMessageModel();
        nmm.setConversationType(String.valueOf(message.getConversationType()));
        nmm.setTargetId(message.getTargetId());
        nmm.setSenderUserId(message.getSenderUserId());
        nmm.setNotificationContent(contentText);
        nmm.setNotifiState(true);
        nmm.setNotifiWhere(false);
        nmm.setUserInfo(userInfo);
        MessageContent messageContent = message.getContent();
        if (messageContent instanceof RichContentMessage) {
            RichContentMessage contentMessage = (RichContentMessage) messageContent;
            nmm.setOnlyContent(contentMessage.getUrl());
        } else {
            nmm.setOnlyContent("");
        }
        baseNotification(nmm);
    }

    private void baseNotification(NotificationMessageModel nmm) {
        Intent intent = new Intent();
        Uri uri;
        intent.setAction(Intent.ACTION_VIEW);
        uri = Uri.parse("rong://" + RongContext.getInstance().getPackageName()).buildUpon().appendPath("conversation")
                .appendPath(nmm.getConversationType())
                .appendQueryParameter("targetId", nmm.getTargetId()).build();
        intent.setData(uri);
        intent.putExtra(Globals.NOTIFICATIONSTATE, nmm.isNotifiState());
        intent.putExtra(Globals.NOTIFICATIONWHERE, nmm.isNotifiWhere());
        CommonNotification commonNotification = new CommonNotification(intent, "办公浏览器", nmm.getNotificationContent(), nmm.getOnlyContent(), nmm.getUserInfo(), shouldSound, 0);
        commonNotification.showNotification();
    }

    /**
     * 消息分类提醒
     *
     * @param message
     * @return
     */
    public String MessageType(MessageContent message) {
        String contextStr = "";
        if (message instanceof TextMessage) {//文本消息
            TextMessage textMessage = (TextMessage) message;
            contextStr = textMessage.getContent();
        } else if (message instanceof ImageMessage) {//图片消息
            contextStr = "[图片]";
        } else if (message instanceof VoiceMessage) {//语音消息
            contextStr = "[语音]";
        } else if (message instanceof RichContentMessage) {//图文消息
            RichContentMessage richContentMessage = (RichContentMessage) message;
            contextStr = richContentMessage.getContent();
//            contextStr = "[图文]";
        } else if (message instanceof LocationMessage) {
            contextStr = "[位置]";
        }
        return contextStr;
    }

    public static class Builder {
        private Context context = null;
        private boolean shouldSound = false;

        public Builder setContext(Context context) {
            this.context = context;
            return this;
        }

        public Builder setSound(boolean shouldSound) {
            this.shouldSound = shouldSound;
            return this;
        }

        public EnterplorerNotificationManager build() {
            return new EnterplorerNotificationManager(this);
        }
    }
}
